package com.dtlaya.allutils.dcmFileUpload.controller;

import com.dtlaya.allutils.dcmFileUpload.service.DicomStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class DicomUploadController {

    private final DicomStorageService dicomStorageService;

    @Autowired
    public DicomUploadController(DicomStorageService dicomStorageService) {
        this.dicomStorageService = dicomStorageService;
    }

    @PostMapping("/dicom/upload")
    public ResponseEntity<Map<String, Object>> uploadDicomFiles(
            @RequestParam("files") MultipartFile[] files) {
        
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 存储 DICOM 文件
            List<Path> storedPaths = dicomStorageService.storeDicomFiles(files);
            
            response.put("success", true);
            response.put("message", "DICOM 文件上传成功");
            response.put("count", storedPaths.size());
            response.put("paths", storedPaths.stream()
                    .map(Path::toString)
                    .collect(Collectors.toList()));
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("success", false);
            response.put("message", "DICOM 文件上传失败: " + e.getMessage());
            return ResponseEntity.badRequest().body(response);
        }
    }
}